<?php
/**
 *
 * @author 凡墙<jihaoju@qq.com>
 * @date 2018/7/29 10:36
 * @description
 */

namespace apps\marriage\core\logic;


use think\Exception;

class SmsLogic
{
    /**
     * 发送验证码短信
     * @param $phone
     * @param $type
     * @return mixed
     */
    public function sendCaptcha($type, $phone){
        $sms = array(
            'type' => $type,
            'phone' => $phone
        );
        return base_api_post('base', '/sms_captcha/send', $sms);
    }

    /**
     * 验证手机短信验证码
     * 验证成功后将删除缓存
     * @param $phone
     * @param $type
     * @param $captcha
     * @return bool
     * @throws Exception
     */
    public function verifyCaptcha($type, $phone, $captcha)
    {
        $ret = base_api_get('base', '/sms_captcha/verify', [
            'phone' => $phone,
            'type' => $type,
            'captcha' => $captcha
        ]);
        if($ret['result'] == 1) {
            return true;
        }
        throw new Exception('验证码错误');
    }


    /**
     * 验证手机短信验证码
     * 验证成功后不会删除缓存
     * @param $phone
     * @param $type
     * @param $captcha
     * @return bool
     * @throws Exception
     */
    public function checkCaptcha($type, $phone, $captcha)
    {
        $ret = base_api_get('base', '/sms_captcha/check', [
            'phone' => $phone,
            'type' => $type,
            'captcha' => $captcha
        ]);
        if($ret['result'] == 1) {
            return true;
        }
        throw new Exception('验证码错误');
    }

}